package com.zhn.ui;

import com.zhn.util.EncrUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

/**
 * 加密解密的简单UI界面
 */
public class GUIExample extends JFrame {
    private JTextField inputField;
    private JTextArea outputArea;
    private static final String KEY = "ThisIsSecretKey1234567890";
    private static byte[] MIMA ;
    public GUIExample() {
        setTitle("Java图形界面示例");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        inputField = new JTextField("在当前输入框输入加密内容");
        inputField.setPreferredSize(new Dimension(300, 30));
        panel.add(inputField, BorderLayout.NORTH);

        outputArea = new JTextArea();
        outputArea.setPreferredSize(new Dimension(300, 200));
        panel.add(outputArea, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());

        JButton resetButton = new JButton("重置");
        resetButton.setPreferredSize(new Dimension(80, 30));
        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                inputField.setText("");
                outputArea.setText("");
            }
        });
        buttonPanel.add(resetButton);

        JButton submitButton = new JButton("加密");
        submitButton.setPreferredSize(new Dimension(80, 30));
        submitButton.addActionListener((e)->{
                String input = inputField.getText();
                String processedInput = null;
                try {
                    processedInput = processInput(input);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                outputArea.append("加密后的内容：" + processedInput + "\n");
                inputField.setText("");
        });
        buttonPanel.add(submitButton);

        JButton submitButton1 = new JButton("解密");
        submitButton1.setPreferredSize(new Dimension(80, 30));
        submitButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String processedInput = null;
                try {
                    processedInput = processOutput();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                outputArea.append("解密后的内容：" + processedInput + "\n");
                inputField.setText("");
            }
        });
        buttonPanel.add(submitButton1);


        panel.add(buttonPanel, BorderLayout.SOUTH);

        add(panel);
        setVisible(true);
    }

    private String processInput(String input) throws Exception {
        // 在这里进行对输入内容的处理，这里只是一个示例，你可以根据需要自定义处理逻辑
        String key = "ThisIsSecretKey1234567890";
        byte[] encrypt = EncrUtil.encrypt(input, key);
        MIMA = encrypt;
        return encrypt.toString();
    }
  private String processOutput() throws Exception {
        // 在这里进行对输入内容的处理，这里只是一个示例，你可以根据需要自定义处理逻辑
      String decrypt = EncrUtil.decrypt(MIMA, KEY);
      return decrypt;
    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GUIExample();
            }
        });
    }
}